package filter.session;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class Session3 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //解决编码显示问题
        req.setCharacterEncoding("gbk");
        resp.setCharacterEncoding("gbk");
        //指定显示
        resp.setContentType("text/html;charset=gbk");
        //得到session
        HttpSession session = req.getSession();
        //给session存东西
        session.setAttribute("name",new Person("浩海",18,"男"));

        //获取id
        String id = session.getId();

        //判断session是否是新创建
        if (session.isNew())
        {
            resp.getWriter().write("session创建成功,ID:"+id);
        }else
        {
            resp.getWriter().write("session已在服务器中存在了，ID："+id);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
